package com.hfxt.service.impl;
import com.hfxt.constants.Constants;
import com.hfxt.constants.PayChannelEnum;
import com.hfxt.dto.MaintainOrderDto;
import com.hfxt.mapper.PayOrderMapper;
import com.hfxt.po.PayOrder;
import com.hfxt.service.PayOrderService;
import com.hfxt.dto.Page;
import com.hfxt.utils.DateUtils;
import com.hfxt.utils.MathUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
@Service
public class PayOrderServiceImpl implements PayOrderService {

    Logger log = LoggerFactory.getLogger(PayOrderServiceImpl.class);

    @Resource
    private PayOrderMapper payOrderMapper;

    public PayOrder getPayOrderById(Long id){
        return payOrderMapper.getPayOrderById(id);
    }

    public List<PayOrder>	getPayOrderListByMap(Map<String,Object> param){
        return payOrderMapper.getPayOrderListByMap(param);
    }

    public Integer getPayOrderCountByMap(Map<String,Object> param){
        return payOrderMapper.getPayOrderCountByMap(param);
    }

    public Integer qdtxAddPayOrder(PayOrder payOrder){
            payOrder.setCreatdTime(new Date());
            return payOrderMapper.insertPayOrder(payOrder);
    }

    public Integer qdtxModifyPayOrder(PayOrder payOrder){
        payOrder.setUpdatedTime(new Date());
        return payOrderMapper.updatePayOrder(payOrder);
    }

    public Integer qdtxDeletePayOrderById(Long id){
        return payOrderMapper.deletePayOrderById(id);
    }

    public Integer qdtxBatchDeletePayOrder(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return payOrderMapper.batchDeletePayOrder(param);
    }

    public Page<PayOrder> queryPayOrderPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = payOrderMapper.getPayOrderCountByMap(param);
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<PayOrder> payOrderList = payOrderMapper.getPayOrderListByMap(param);
        page.setList(payOrderList);
        return page;
     }

    /**
     * 保存交易支付订单
     *
     * @param maintainOrder
     * @param payChannelEnum
     * @param price
     * @return
     * @
     */
    @Override
    public PayOrder save(MaintainOrderDto maintainOrder, PayChannelEnum payChannelEnum, BigDecimal price) {

        String payNo = maintainOrder.getOrderNo();
        Integer payType = null;
        if (maintainOrder.getStatus().equals(Constants.Order.MaintainStatus.APPOINT)) {
            payType = Constants.Order.MaintainStatus.PAY_DEPOSIT;
        }
        // 创建支付订单编号啊
        payNo = createPayNo(payNo, payType);
        PayOrder payOrder = new PayOrder(maintainOrder.getOrderNo(), payNo, maintainOrder.getUserId()
                , price, payType, payChannelEnum.getCode());
        payOrderMapper.insertPayOrder(payOrder);
        return payOrder;
    }

    /**
     * 创建支付订单编号
     * 支付订单编号根据订单编号 + 随机数 + 状态类型 生成
     *
     * @param payNo
     * @param payType
     * @return
     */
    private String createPayNo(String payNo, Integer payType) {
        StringBuffer buffer = new StringBuffer(payNo);
        buffer.append(MathUtils.random());
        buffer.append(payType);
        log.info("订单：{}当前状态为：{}，生成支付订单编号：{}", payNo, payType, buffer);
        PayOrder payOrder = payOrderMapper.selectByPayNo(buffer.toString());
        // 如果已经存在，那么重新生成一个支付订单编号
        if (payOrder != null) {
            return createPayNo(payNo, payType);
        }
        return buffer.toString();
    }

    /**
     * 支付完成，修改订单状态
     *
     * @param payOrder
     * @param aliTradeNo
     */
    @Override
    public void finishPay(PayOrder payOrder, String aliTradeNo) {
        Date currentTime = DateUtils.getCurrentDate(DateUtils.YYYY_MM_DD_HH_MM_SS);
        payOrder.setAliTradeNo(aliTradeNo);
        payOrder.setStatus(Constants.Order.PayStaus.SUCCESS);
        payOrder.setUpdatedUserId(payOrder.getUserId());
        payOrder.setUpdatedTime(currentTime);
        payOrderMapper.updatePayOrder(payOrder);
    }


}
